package com.world.cat.controller.front;

import com.world.cat.model.sys.User;
import com.world.cat.service.sys.UserService;
import com.world.common.pojo.Result;
import com.world.common.util.SysUtil;
import org.apache.log4j.Logger;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController {
    private static final Logger logger = Logger.getLogger(LoginController.class);
    @Resource
    private AuthenticationManager myAuthenticationManager;
    @Resource
    private UserService userService;

    //进入登录页
    @RequestMapping(value = "/home")
    public String loginEntry() {
        return "login/home";
    }

    //登陆认证
    @RequestMapping(value = "/go")
    @ResponseBody
    public Object go(HttpServletRequest request, HttpSession session, String j_username, String j_password) {
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(j_username, j_password);
        try {
            UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) myAuthenticationManager.authenticate(authRequest); //调用loadUserByUsername
            //设置authentication中details  
            authentication.setDetails(new WebAuthenticationDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authentication);
            session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext()); // 这个非常重要，否则验证后将无法登陆
            User user = userService.findByUsername(j_username);
            session.setAttribute("avatar", user.getAvatar());
            return new Result(true, "登陆成功");
        } catch (AuthenticationException ex) {
            logger.error("登陆失败", ex);
            return new Result(false, "登陆失败请检查用户名密码");
        }
    }

    @RequestMapping(value = "/usernameNotExist")
    @ResponseBody
    public Object usernameNotExist(User user) {
        if (SysUtil.isEmpty(user.getUsername())) {
            return true;
        }
        try {
            User userResult = userService.findByUsername(user.getUsername());
            if (userResult == null) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        return false;
    }

    @RequestMapping(value = "/usernameExist")
    @ResponseBody
    public Object usernameExist(User user) {
        boolean xx = (boolean) usernameNotExist(user);
        return !xx;
    }
}
